package main;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import aisLogic.Module;

public class AISModuleButtonPanel extends JPanel implements ActionListener{
	
	AISModuleScrollPane scrollPane;
	
	public AISModuleButtonPanel(AISModuleScrollPane arg0){
		super(new GridLayout(0,2));
		scrollPane = arg0;
		JButton addModule = new JButton("Add");
		JButton delModule = new JButton("Delete");
		JButton cloneModule = new JButton("Clone");
		JButton moveUpModule = new JButton("Move Up");
		JButton moveDownModule = new JButton("Move Down");
		addModule.addActionListener(this);
		addModule.setActionCommand("add");
		delModule.addActionListener(this);
		delModule.setActionCommand("delete");
		cloneModule.addActionListener(this);
		cloneModule.setActionCommand("clone");
		moveUpModule.addActionListener(this);
		moveUpModule.setActionCommand("up");
		moveDownModule.addActionListener(this);
		moveDownModule.setActionCommand("down");
        add(addModule);
        add(delModule);
        add(cloneModule);
        add(moveUpModule);
        add(moveDownModule);
	}
	
	public void actionPerformed(ActionEvent arg0) {
		int row = scrollPane.getTable().getSelectedRow();
		int row_count = scrollPane.getTable().getRowCount();

		if(arg0.getActionCommand().equals("add")){
			AthenaMain.manager.addModule(new Module(0, 0, 0, 0));
		} else if (arg0.getActionCommand().equals("delete")){
			if(row_count == 0){
				return;
			} else {
				if(row == -1){
					AthenaMain.manager.removeModule(row_count - 1);
				} else {
					AthenaMain.manager.removeModule(row);
				}
			}
		} else if (arg0.getActionCommand().equals("clone")){
		} else if (arg0.getActionCommand().equals("up")){
			if(row > 0 && row != -1){
				AthenaMain.manager.moduleUp(row);
			}
		} else if (arg0.getActionCommand().equals("down")){
			if(row < row_count - 1 && row != -1){
				AthenaMain.manager.moduleDown(row);
			}
		}
	}
}
